/*
 * SearchRestController.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.web.api;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.onesalt.domain.Kitchen;
import com.onesalt.domain.User;
import com.onesalt.service.KitchenService;
import com.onesalt.service.UserService;
import com.onesalt.web.api.response.CityListResponse;
import com.onesalt.web.api.response.KitchenListResponse;
import com.onesalt.web.api.response.SearchResponse;
import com.onesalt.web.api.response.UserListResponse;

/**
 * 
 *
 * @author kiri
 * @date 2015年5月10日
 * @version 1.0
 */
@RestController
@RequestMapping("/api/search")
public class SearchRestController extends AbstractRestController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private KitchenService kitchenService;
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public SearchResponse searchByKeyword(@RequestParam String keyword) {
		SearchResponse response = new SearchResponse();
		
		boolean hasMoreCities = false;
		List<String> cities = userService.getKitchenRepository().findAllCities();
		List<String> result = new ArrayList<String>();
		for (String string : cities) {
			if (string.contains(keyword)) {
				if (result.size() == 3) {
					hasMoreCities = true;
					break;
				}
				result.add(string);
			}
		}
		response.setCities(result);
		response.setHasMoreCities(hasMoreCities);
		
		try {
			Page<User> cooks = userService.findAllCooks(null, keyword, null, null, null, null, null, null, null, null, null, null, null, null, 0, 3);
			if (cooks != null) {
				response.setCooks(cooks.getContent());
				response.setHasMoreCooks(!cooks.isLast());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			Page<Kitchen> kitchens = kitchenService.findAll(null, null, null, null, keyword, null, null, null, null, null, null, null, null, null, 0, 3);
			if (kitchens != null) {
				response.setKitchens(kitchens.getContent());
				response.setHasMoreKitchens(!kitchens.isLast());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}

	@RequestMapping(value = "/cities", method = RequestMethod.GET)
	public CityListResponse searchCityByKeyword(@RequestParam(required = false) String keyword, @RequestParam int page, @RequestParam int size) {
		List<String> cities = userService.getKitchenRepository().findAllCities();
		List<String> result = new ArrayList<String>();
		for (String string : cities) {
			if (string.contains(keyword)) {
				result.add(string);
			}
		}
		CityListResponse response = new CityListResponse();
		response.setCities(result);
		response.setLast(true);
		return response;
	}

	@RequestMapping(value = "/cooks", method = RequestMethod.GET)
	public UserListResponse searchCookByKeyword(@RequestParam(required = false) String keyword, @RequestParam int page, @RequestParam int size) {
		UserListResponse response = new UserListResponse();
		Page<User> cooks = userService.findAllCooks(null, keyword, null, null, null, null, null, null, null, null, null, null, null, null, page, size);
		if (cooks != null) {
			response.setUsers(cooks.getContent());
			response.setPage(cooks.getNumber());
			response.setLast(cooks.isLast());
		}
		return response;
	}

	@RequestMapping(value = "/kitchens", method = RequestMethod.GET)
	public KitchenListResponse searchKitchenByKeyword(@RequestParam(required = false) String keyword, @RequestParam int page, @RequestParam int size) {
		KitchenListResponse response = new KitchenListResponse();
		Page<Kitchen> kitchens = kitchenService.findAll(null, null, null, null, keyword, null, null, null, null, null, null, null, null, null, page, size);
		if (kitchens != null) {
			for (Kitchen kitchen : kitchens) {
				kitchen.setCook(getUserService().getUserRepository().findOne(kitchen.getCookId()));
			}
			response.setKitchens(kitchens.getContent());
			response.setPage(kitchens.getNumber());
			response.setLast(kitchens.isLast());
		}
		return response;
	}
}
